﻿using Microsoft.AspNetCore.DataProtection.KeyManagement;

namespace Nasty.Core.SuperExtension
{
    public static class FileExtension
    {
        public static (string Name, string Extension) NameExtension(this string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
                return (string.Empty, string.Empty);

            int lastDot = fileName.LastIndexOf('.');
            if (lastDot > 0 && lastDot < fileName.Length - 1)
            {
                return (fileName.Substring(0, lastDot), fileName.Substring(lastDot + 1));
            }
            else
            {
                return (fileName, string.Empty);
            }
        }


        public static FormFile ToFormFile(this byte[] buffer, string fileName, string contentType)
        {
            using var uploadStream = new MemoryStream(buffer);
            var file = new FormFile(uploadStream, 0, buffer.Length, "file", fileName)
            {
                Headers = new HeaderDictionary(),
                ContentType = contentType
            };

            return file;
        }
    }
}
